package server.util;
import com.sun.net.httpserver.*;
import server.database.RegistrationDatabase;
import server.handler.Handler;

import java.sql.SQLException;

public class AuthorizationUtil extends BasicAuthenticator{
    static private RegistrationDatabase db = RegistrationDatabase.getInstance();
    public AuthorizationUtil (String route){
        super(route);
    }

    public boolean checkCredentials(String userName, String password){
        try{
            if(!db.hasUsername(userName)){
                System.out.println("Please register as a user first");
                return false;
            }
        }catch (Exception e){
            System.out.println(e.getMessage());
            return false;
        }
        try{
            password = MD5Util.encryMD5(password);
        }catch (Exception e){
            System.out.println(e.getMessage());
            return false;
        }
        String truePassword;
        try {
            truePassword = db.getRegistrationsByUsername(userName);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return false;
        }
        if(!truePassword.equals(password)){
            System.out.println("secret word");
            return false;
        }
        return true;
    }
}
